package com.lefevre.cms.bean.question;

import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 答案
 *
 * @author Lefevre
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@TableName("answer")
@Entity
@Table(name = "answer", indexes = {@Index(name = "answer_1_idx", columnList = "questionId,status,adoption"), @Index(name = "answer_2_idx", columnList = "userName,isStaff")})
public class Answer implements Serializable {
    private static final long serialVersionUID = -7929411590314542710L;

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @TableId(type = IdType.INPUT)
    private Long id;

    /**
     * 是否是员工   true:员工   false:会员
     **/
    @TableField("isStaff")
    private Boolean isStaff = false;

    /**
     * 用户名称
     **/
    @Column(length = 30)
    @TableField("userName")
    private String userName;

    /**
     * 呢称
     **/
    @Transient
    @TableField(exist = false)
    private String nickname;

    /**
     * 头像路径
     **/
    @Transient
    @TableField(exist = false)
    private String avatarPath;

    /**
     * 头像名称
     **/
    @Transient
    @TableField(exist = false)
    private String avatarName;

    /**
     * IP
     **/
    @Column(length = 45)
    @TableField("ip")
    private String ip;

    /**
     * IP归属地
     **/
    @Transient
    @TableField(exist = false)
    private String ipAddress;

    /**
     * 问题Id
     **/
    @TableField("questionId")
    private Long questionId;

    /**
     * 问题标题
     **/
    @Transient
    @TableField(exist = false)
    private String questionTitle;

    /**
     * 状态 10.待审核 20.已发布 110.待审核用户删除 120.已发布用户删除 100010.待审核员工删除 100020.已发布员工删除
     **/
    @TableField("status")
    private Integer status = 10;

    /**
     * 答案是否采纳
     **/
    @TableField("adoption")
    private Boolean adoption = false;

    /**
     * 答案内容
     **/
    @Lob
    @TableField("`content`")
    private String content;

    /**
     * 回答时间
     **/
    @Temporal(TemporalType.TIMESTAMP)
    @TableField("postTime")
    private Date postTime = new Date();

    /**
     * 最后修改时间
     **/
    @Temporal(TemporalType.TIMESTAMP)
    @TableField("lastUpdateTime")
    private Date lastUpdateTime;

    /**
     * 用户角色名称集合
     **/
    @Transient
    @TableField(exist = false)
    private List<String> userRoleNameList = new ArrayList<>();

    /**
     * 总回复数
     **/
    @Transient
    @TableField(exist = false)
    private Integer totalReply = 0;

    /**
     * 回复集合
     **/
    @Transient
    @TableField(exist = false)
    private List<AnswerReply> answerReplyList = new ArrayList<>();

    /**
     * 添加 回复
     *
     * @param answerReply
     */
    public void addAnswerReply(AnswerReply answerReply) {
        this.setTotalReply(this.getTotalReply() + 1);
        this.answerReplyList.add(answerReply);
    }

}
